package gemini.pollux.ui.client.page.content.product.product.edit;

import gemini.basic.model.Product;
import gemini.basic.model.ProductType;
import gemini.pollux.ui.client.ServiceCatalog;
import gemini.pollux.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetDisplay;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetPresenter;
import gemini.pollux.ui.client.page.RootEvent;
import gemini.pollux.ui.client.page.content.product.product.ProductEvent;
import gemini.pollux.ui.client.page.content.product.product.ProductObject;
import gemini.pollux.ui.client.utils.GuiUtils;
import gemini.pollux.ui.client.utils.UIConstants;

import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.inject.Inject;

public class ProductEditPresenter extends
		PolluxWidgetPresenter<ProductEditPresenter.Display> {

	@Inject
	public ProductEditPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends PolluxWidgetDisplay {
		HasClickHandlers getBackClickHandlers();

		HasClickHandlers getSubmitClickHandlers();

		Product getProduct();

		void setProduct(Product product);

		void setProductTypes(List<ProductType> productType);

		FormPanel getFormPanel();
	}

	public static final Place PLACE = new Place(UIConstants.PLACE_PRODUCT);

	@Override
	protected void onBind() {
		registerHandler(display.getBackClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new ProductEvent(
								ProductEvent.ForwardType.BACK_TO_LIST));
					}
				}));
		
		registerHandler(display.getSubmitClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						eventBus.fireEvent(new RootEvent(
								RootEvent.ForwardType.MASK));
						ServiceCatalog.getProductService().saveOrUpdateProduct(
								display.getProduct(),
								new AbstractAsyncCallback<Product>() {

									@Override
									public void onSuccess(Product result) {
										eventBus.fireEvent(new RootEvent(
												RootEvent.ForwardType.UN_MASK));
										product = result;
										confirmBackToList();
									}
								});
					}
				}));

		display.setProductTypes(productTypes);
		display.setProduct(product);
		display.initCustom();
	}
	
	private List<ProductType> productTypes;

	private void confirmBackToList() {
		String message = "Save or update product successfull!";

		GuiUtils.info(message);

		localEventBus.fireEvent(new ProductEvent(
				ProductEvent.ForwardType.BACK_TO_LIST));
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	private Product product;

	@Override
	public void setInputObject(Object ob) {
		ProductObject lvObject = (ProductObject) ob;
		if (lvObject != null) {
			this.product = lvObject.getProduct();
			this.productTypes = lvObject.getProductTypes();
		}
	}

	@Override
	public void onUnbind() {
		product = null;
		productTypes = null;
	}
}
